﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Cit.CDE.Net.Module;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using ConfigAndElse;
using System.Text;
using cde.UserControl;
using System.IO;
using System.Xml;
using Module;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Service;
using UserControl;
using System.Timers;
using System.Data.SqlClient;
using Cit.CDE.ApplicationBlocks.Data;
using Cit.CDE.Net.Service.DataAccess;
using System.Reflection;
using System.Net.Mail;

/// <summary>
/// FileName:       Business_Detail.ascx
/// Author :         wangbc
/// CDT:             2013-8-5           
/// Version:         1.0.0.0                
/// Depiction:       商机详细
/// </summary>
public partial class main_UserControls_Business_Detail : UserControl.UserControlsCommon
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 查询条件
    /// </summary>
    private  CDESearchCriteria sc;

    private CDEEntity m_entity;
    private string p_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField hd = (HiddenField)this.Parent.FindControl("BS_ID");
        p_id = hd.Value;
        if (!IsPostBack)
        {
            em = CDEEMFactory.getEMByFullName(WEBConstants.EM_BUSINESS_DETAILS_MANAGE);
            sc = new CDESearchCriteria(em);
            m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
            m_entity.Operation = CDEDataOperation.INSERT;
            Session["MENTITY_OTHERCONN" + p_id] = m_entity;
            //信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
      
        if (p_id == "")
        {
            p_id = "0";
        }
        this.bindDataByID(p_id);
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void btnSave_Click(object sender, EventArgs e)
    {
        em = CDEEMFactory.getEMByFullName(WEBConstants.EM_BUSINESS_DETAILS_MANAGE);
        List<CDEEntity> listEntity = new List<CDEEntity>();    
        HiddenField hd = (HiddenField)this.Parent.FindControl("BS_ID");
        string pre_id = hd.Value;
        m_entity = (CDEEntity)Session["MENTITY_OTHERCONN"+pre_id];
        //页面信息放入催收状态Entity
        this.setEntityFromRequest(m_entity, Request.Params, "uc_Business_Detail");
        m_entity.set("BS_ID", pre_id);       
        listEntity.Add(m_entity);
        CDEService.UpdateEntities(em, listEntity);
        //判断是否出错
        if (listEntity[0].Result == CDEDataOperationResult.ERROR)
        {           
            this.lbl_message.Text = "保存失败";
        }
        else
        {                
            this.bindDataByID(pre_id);
            this.div_Business_Detail.Style.Add("Display", "none");
        }       
    }

    /// <summary>
    /// 数据绑定样式
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dgmeisai_ItemDataBound(object sender, DataGridItemEventArgs e)
    {       
        int index = e.Item.ItemIndex;
        if (index == -1 || dgmeisai.ListEntity == null || dgmeisai.ListEntity.Count < index) return;
        e.Item.Attributes.Add("style","text-align :center");
        //设置鼠标滑动效果
        if (index % 2 == 0)
        {
            e.Item.Attributes.Add("class", "bgtr2");
            e.Item.Attributes.Add("onmouseover", "this.className='bgtr3'");
            e.Item.Attributes.Add("onmouseout", "this.className='bgtr2'");
        }
        else
        {
            e.Item.Attributes.Add("class", "bgtr1");
            e.Item.Attributes.Add("onmouseover", "this.className='bgtr3'");
            e.Item.Attributes.Add("onmouseout", "this.className='bgtr1'");
        }

        //MethodInfo mi = this.buttonArea.Page.GetType().GetMethod("itemDataBound"); //获取SetValue()方法

        //object[] per = new object[]{sender,e};   //建造输入参数

        //mi.Invoke(this.buttonArea.Page, per);    //调用SetValue方法
    }

    /// <summary>
    /// 绑定页面数据
    /// </summary>
    public void bindDataByID(string pre_id)
    {
        em = CDEEMFactory.getEMByFullName(WEBConstants.EM_BUSINESS_DETAILS_MANAGE);
        sc = new CDESearchCriteria(em);
        sc.addCondition("BS_ID", pre_id);
        List<CDEEntity> list = CDEService.LoadEntities(em, sc);
        if (this.dgmeisai.Columns.Count > 1)
        {
            DataGridColumn col = this.dgmeisai.Columns[this.dgmeisai.Columns.Count-1];
            this.dgmeisai.Columns.Clear();
            col.HeaderStyle.Width = 100;
            col.ItemStyle.Width = 100;
            this.dgmeisai.Columns.Add(col);
            //for (int i = 1; i < this.dgmeisai.Columns.Count; )
            //{
            //    this.dgmeisai.Columns.RemoveAt(i);
            //}            
        }        
        this.dgmeisai.EntityManager = em;       
        this.dgmeisai.ListEntity = list;
        this.dgmeisai.DataBind();
        this.div_Business_Detail.Style.Add("Display", "none");
    }


    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string operate = btn.ID.ToString();
        string id = ((HiddenField)btn.Parent.FindControl("hidValueId")).Value;

        em = CDEEMFactory.getEMByFullName(WEBConstants.EM_BUSINESS_DETAILS_MANAGE);
        sc = new CDESearchCriteria(em);
        sc.addCondition("ID", id);
        List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
        if (listEntity.Count > 0)
        {
            m_entity = listEntity[0];
            m_entity.Operation = CDEDataOperation.UPDATE;
            Session["MENTITY_OTHERCONN" + p_id] = m_entity;
            //信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);

            this.btnSave.Text = "修改明细";
            this.div_Business_Detail.Style.Add("Display","block") ;
        }
    }


    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void btnAdd_Click(object sender, EventArgs e)
    {
        sc = new CDESearchCriteria(em);
        m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
        m_entity.Operation = CDEDataOperation.INSERT;
        Session["MENTITY_OTHERCONN" + p_id] = m_entity;
        //信息设值到页面
        this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);

        this.btnSave.Text = "新增明细";
        this.div_Business_Detail.Style.Add("Display", "block");
    }


    /// <summary>
    /// 删除 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string operate = btn.ID.ToString();
        string id = ((HiddenField)btn.Parent.FindControl("hidValueId")).Value;

        em = CDEEMFactory.getEMByFullName(WEBConstants.EM_BUSINESS_DETAILS_MANAGE);
        sc = new CDESearchCriteria(em);
        sc.addCondition("ID", id);
        List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
        if (listEntity.Count > 0)
        {
            m_entity = listEntity[0];
            m_entity.Operation = CDEDataOperation.DELETE;
            CDEService.UpdateEntities(em, listEntity);       
            this.bindDataByID(p_id);
        }
    }
}
